#include "InputBox.h"

namespace jm4bbx {

	const groupType InputBox::OUTPUT_GROUP = groupType(0);
	const columnType InputBox::OUTPUT_COLUMN = columnType(0);
	const outType InputBox::OUT_ARC = outType(0);	
	const char* InputBox::INPUT_SPECIFICATION_PARAMETER_NAME = "specification";


	InputBox::InputBox(const BoxParamPack& paramsPack) 
		: RootBox(paramsPack)
		, inputSpecification_("") {
			
			inputSpecification_ = getBoxParameterValue(paramsPack, 
					INPUT_SPECIFICATION_PARAMETER_NAME, std::string(""));

			for (size_t i = 0; i < inputSpecification_.size(); ++i) {
				if (inputSpecification_[i] == BLANK_SPACE_PROXY_CHARACTER) {
					inputSpecification_[i] = ' ';
				} 
				else if (inputSpecification_[i] == BACKSLASH_PROXY_CHARACTER) {
					inputSpecification_[i] = '\\';
				}
			}
	}



	void InputBox::initImpl() {
		add_group(OUTPUT_GROUP, OUTPUT_COLUMN, 
			get_output_descriptors(OUT_ARC));
		initDataStructures();			
		wait4SignalsIn();
	}

	void InputBox::syncMachEtwas() {
		
		//GroupPtr ig = getGroup(INPUT_GROUP);
		//if (ig->isPoisoned() || areSignalsInPoisoned()) {				
		//	send_poisoned(OUT_ARC);			
		//	sendSignalsOut(true);
		//	wait4Receive();
		//	return;
		//}

		//LOG_I("Load data")

		loadData();
		
		//LOG_I("data loaded")

		const size_t size = getColumnSize();
		//LOG_I("Alokacia " << size << " riadkov.")
		getGroupPtr(OUTPUT_GROUP)->resize(size);
		//LOG_I("Alokacia " << size << " riadkov ukoncena")
		//LOG_I("Send data")
		send(OUT_ARC, OUTPUT_GROUP);
		//LOG_I("Sended data")
		sendSignalsOut();
		wait4SignalsIn();
	}

	void InputBox::initDataStructures() {
		const size_t size = getColumnSize();
		getGroupPtr(OUTPUT_GROUP)->allocate(size);
	}

} // namespace jm4bbx
